package com.stage.battle
{
	import com.stage.item.Player;
	import com.utils.time.TimerClock;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * 战斗场景
	 * 注意，注意放的应该是一些显示对象，不要把过多逻辑内容放在这里
	 */
	public class BattleField extends Sprite
	{	
		private var _backLayer:Sprite = new Sprite;
		private var _middleLayer:Sprite = new Sprite;
		private var _frontLayer:Sprite = new Sprite;
		private var _battleFieldWidth:Number;
		private var _battleFieldHeight:Number;
		
		private var _lockTarget:Player;
		private var _lockPoint:Point = new Point(0.5, 0.8); // 锁定到的位置
		private var _toScale:Number = 1;
		private var _scaleSpeed:Number = 0.05;
		private var _threadId:int = 0;
		
		private static var _instance:BattleField;
		/**
		 * 战斗场景是唯一的
		 */
		public static function getInstance():BattleField{
			if(!_instance){
				_instance = new BattleField; 
			}
			return _instance;
		}
		
		public function BattleField()
		{
			super();
			addChild(_backLayer);
			addChild(_middleLayer);
			addChild(_frontLayer);
			BattleTextManager.getInstance().container = _frontLayer;
			
		}
		
		/**
		 * 战斗开始前初始化
		 * 初始化一些战场对象，如背景等
		 */
		public function init():void{
			_battleFieldWidth = 2000;
			_battleFieldHeight = 1000;
			
			graphics.beginFill(0xff00ff, 0.1);
			graphics.drawRect(0, 0, _battleFieldWidth, _battleFieldHeight);
			graphics.endFill();
			
			x = 0;
			y = 0;
			scaleX = 1;
			scaleY = 1;
			
			// TODO 初始化战斗位置
			TimerClock.getInstance().addEnterFrame(enterFrame);
		}
		
		/**
		 * 战斗结束，清理战场
		 */
		public function clear():void{
			if(_threadId > 0){
				TimerClock.getInstance().removeEnterFrame(_threadId);
				_threadId = 0;
			}
			_lockTarget = null;
		}
		
		private function enterFrame():void{
			// 放大视角
			if(scaleX < _toScale){
				if(scaleX + _scaleSpeed < _toScale){
					scaleX += _scaleSpeed;
				}else{
					scaleX = _toScale;
				}
			}else if(scaleX > _toScale){
				if(scaleX - _scaleSpeed > _toScale){
					scaleX -= _scaleSpeed;
				}else{
					scaleX = _toScale;
				}
			}
			scaleY = scaleX; // 这里注意：假定比例永远成正比
			
			// 锁定视角
			if(_lockTarget){ // 把主角锁定在屏幕中间
				x = stage.stageWidth * _lockPoint.x  - _lockTarget.x * scaleX;
				y = stage.stageHeight * _lockPoint.y - _lockTarget.y * scaleY;
				x = x > 0 ? 0 : (x < stage.stageWidth - _battleFieldWidth * scaleX ? stage.stageWidth - _battleFieldWidth * scaleX : x);
				y = y > 0 ? 0 : (y < stage.stageHeight - _battleFieldHeight * scaleY ? stage.stageHeight - _battleFieldHeight * scaleY : y);
			}
			
		}
		
		/**
		 * 视角锁定目标
		 */
		public function lockToPlayer(player:Player):void{
			_lockTarget = player;
		}
		
		/**
		 * 放大视角
		 */
		public function set toScale(value:Number):void{
			_toScale = value;
		}
		
		public function set lockPoint(value:Point):void{
			if(value){
				_lockPoint = value;
			}
		}
		
		public function get battleFieldWidth():Number{
			return _battleFieldWidth;
		}
		
		public function get battleFieldHeight():Number{
			return _battleFieldHeight;
		}
		
		public function get backLayer():Sprite{
			return _backLayer;
		}
		
		public function get middleLayer():Sprite{
			return _middleLayer;
		}
		
		public function get frontLayer():Sprite{
			return _frontLayer;
		}
	}
}